﻿Public Class Frm_khabir
    Public avo As Khabir

    Private Sub Btn_parc_ville_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_ville.Click
        Me.Enabled = False
        Frm_Avoc_Bure_ville.txt_ville.Select()
        Frm_Avoc_Bure_ville.Show()
    End Sub

    Private Sub Btn_ajou_tele_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_tele.Click
        Partager.typePhone = "Tel"
        Me.Enabled = False
        Frm_Tele_Bure.txt_tele.Select()

        '------
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Tele_Bure.List_tele.Items.Clear()
                Partager.list_hatif.Clear()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Tel" Then
                        Frm_Tele_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
        Frm_Tele_Bure.Show()
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        Partager.type = "khzbir"
        Me.Enabled = False
        Frm_Rech_khabir.Show()
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            Partager.close()
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة خبير جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
               
                If (txt_3a2ili.Text <> "" And txt_chakhssi.Text <> "") Then
                    If MsgBox("هل تريد فعلا إضافة هذا الخبير ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة الخبير") = MsgBoxResult.Yes Then

                        Try


                            Dim cli As New Khabir(1, txt_3a2ili.Text, txt_chakhssi.Text, comb_ville.Text, txt_adresse.Text, txt_molahadat.Text)
                            Partager.moi.Ajouter_khabir(cli)
                            'l'objet en cours
                            Dim lastItem As Integer = Partager.moi.List_khabir.Count - 1
                            cli = CType(Partager.moi.List_khabir.Item(lastItem), Khabir)

                            For Each d As Phone In Partager.list_hatif
                                cli.Ajouter_phone(d)
                            Next

                            List_tous.Items.Clear()
                            For Each khib As Khabir In Partager.moi.List_khabir
                                List_tous.Items.Add(New ListViewItem(New String() {khib.Nom, khib.Prenom, khib.Ville, khib.Adress, khib.Remarque}))
                            Next

                            MsgBox("تمت إضافة الخبير بنجاح", MsgBoxStyle.Information, "نجاح")
                            Partager.vider(Me)
                        Catch ex As Exception
                            MsgBox(ex.Message)

                        End Try
                    End If
                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If
            End If
        Catch ex As Exception

            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        If List_tous.SelectedIndices.Count > 0 Then

            If (txt_3a2ili.Text <> "" And txt_chakhssi.Text <> "") Then
                If MsgBox("هل تريد فعلا تعديل هذا الخبير ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل الخبير") = MsgBoxResult.Yes Then
                    Try

                        Partager.close()
                        Dim cli As New Khabir(1, txt_3a2ili.Text, txt_chakhssi.Text, comb_ville.Text, txt_adresse.Text, txt_molahadat.Text)
                        Partager.moi.modifier_khabir(cli, avo.Id)

                        cli.Id = avo.Id
                        If Partager.list_hatif.Count > 0 Then
                            cli.suprimmer_phone(avo.Id, "Tel")
                            For Each d As Phone In Partager.list_hatif
                                cli.Ajouter_phone(d)
                            Next
                        End If

                        List_tous.Items.Clear()
                        For Each khib As Khabir In Partager.moi.List_khabir
                            List_tous.Items.Add(New ListViewItem(New String() {khib.Nom, khib.Prenom, khib.Ville, khib.Adress, khib.Remarque}))
                        Next

                        MsgBox("عملية التعديل تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me)
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                End If
            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Else
            MsgBox("اختر خبير من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Partager.close()
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا الخبير ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف الخبير") = MsgBoxResult.Yes Then
                Try

                    Dim clt As Khabir = Partager.moi.List_khabir(List_tous.SelectedIndices.Item(0))
                    Partager.moi.suprimmer_khabir(avo.Id)

                    List_tous.Items.Clear()
                    For Each khib As Khabir In Partager.moi.List_khabir
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Nom, khib.Prenom, khib.Ville, khib.Adress, khib.Remarque}))
                    Next
                    MsgBox("تمت عملية الحذف بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر خبير من القائمة", MsgBoxStyle.Information, "حذف")
        End If

    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            For Each i As Integer In List_tous.SelectedIndices
                avo = Partager.moi.List_khabir(i)
                txt_3a2ili.Text = avo.Nom
                txt_chakhssi.Text = avo.Prenom
                comb_ville.Text = avo.Ville
                txt_adresse.Text = avo.Adress
                txt_molahadat.Text = avo.Remarque
                avo.remplir_List_list_phone()
            Next
            If List_tous.SelectedIndices.Count <= 0 Then
                Partager.vider(Me)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Frm_khabir_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt_3a2ili.Select()
        Try
            Partager.moi.remplir_list_khabir()
            List_tous.Items.Clear()
            For Each khib As Khabir In Partager.moi.List_khabir
                List_tous.Items.Add(New ListViewItem(New String() {khib.Nom, khib.Prenom, khib.Ville, khib.Adress, khib.Remarque}))
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Partager.com_ville(comb_ville, "Comb_Ville", "Ville")
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
        txt_3a2ili.Select()
    End Sub
End Class